<template>
  <div class="order-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>订单管理</span>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :model="searchForm" :inline="true" class="search-form">
          <el-form-item label="订单号">
            <el-input
              v-model="searchForm.orderNo"
              placeholder="请输入订单号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="会员名称">
            <el-input
              v-model="searchForm.memberName"
              placeholder="请输入会员名称"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select
              v-model="searchForm.status"
              placeholder="请选择订单状态"
              clearable
              style="width: 150px"
            >
              <el-option label="待付款" :value="1" />
              <el-option label="待发货" :value="2" />
              <el-option label="待收货" :value="3" />
              <el-option label="已完成" :value="4" />
              <el-option label="已取消" :value="5" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 订单列表 -->
      <el-table
        :data="orderList"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column prop="orderNo" label="订单号" width="180" />
        <el-table-column prop="memberName" label="会员名称" width="120" />
        <el-table-column prop="totalAmount" label="订单金额" width="120">
          <template #default="{ row }">
            ¥{{ row.totalAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="订单状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getOrderStatusType(row.status)">
              {{ getOrderStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="paymentMethod" label="支付方式" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleProcess(row)">
              处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="pagination-area">
        <el-pagination
          :current-page="pagination.pageNum"
          :page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'

// 订单数据接口
interface Order {
  id: number
  orderNo: string
  memberName: string
  totalAmount: number
  status: number
  paymentMethod: string
  createTime: string
}

// 搜索表单
const searchForm = reactive({
  orderNo: '',
  memberName: '',
  status: undefined
})

// 分页信息
const pagination = reactive({
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)

// 订单列表
const orderList = ref<Order[]>([
  {
    id: 1,
    orderNo: 'ORD20240101001',
    memberName: '张三',
    totalAmount: 299.00,
    status: 2,
    paymentMethod: '微信支付',
    createTime: '2024-01-01 10:30:00'
  },
  {
    id: 2,
    orderNo: 'ORD20240101002',
    memberName: '李四',
    totalAmount: 599.00,
    status: 3,
    paymentMethod: '支付宝',
    createTime: '2024-01-01 11:15:00'
  },
  {
    id: 3,
    orderNo: 'ORD20240101003',
    memberName: '王五',
    totalAmount: 199.00,
    status: 1,
    paymentMethod: '银行卡',
    createTime: '2024-01-01 14:20:00'
  }
])

// 获取订单状态类型
const getOrderStatusType = (status: number) => {
  const types = ['', 'warning', 'primary', 'success', 'info', 'danger']
  return types[status] || 'info'
}

// 获取订单状态文本
const getOrderStatusText = (status: number) => {
  const texts = ['', '待付款', '待发货', '待收货', '已完成', '已取消']
  return texts[status] || '未知'
}

// 搜索
const handleSearch = () => {
  pagination.pageNum = 1
  // 这里可以调用API搜索订单
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    orderNo: '',
    memberName: '',
    status: undefined
  })
  pagination.pageNum = 1
  // 这里可以调用API获取订单列表
}

// 查看订单详情
const handleView = (order: Order) => {
  console.log('查看订单:', order)
}

// 处理订单
const handleProcess = (order: Order) => {
  console.log('处理订单:', order)
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.pageNum = 1
  // 这里可以调用API获取订单列表
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.pageNum = page
  // 这里可以调用API获取订单列表
}

// 初始化
onMounted(() => {
  total.value = orderList.value.length
})
</script>

<style scoped lang="scss">
.order-list {
  .card-header {
    font-weight: bold;
    color: #333;
  }

  .search-area {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 4px;
  }

  .pagination-area {
    margin-top: 20px;
    text-align: right;
  }
}
</style>

